#include "statement.h"
#include "ui_statement.h"

#include <QMessageBox>
#include <QString>
#include <QList>
#include <QDebug>
#include <QPalette>



statement::statement(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::statement)
{
    ui->setupUi(this);


    //QPixmap pixmap = QPixmap("D://Qt//workspace//bookManager//pictures//xiyang.jpg").scaled(this->size());
    QPixmap pixmap = QPixmap("D://CsSoftware//Qt//__MyFiles//workspace//bookManager//pictures//dog.jpg").scaled(this->size());
    QPalette palette(this->palette());
    palette.setBrush(QPalette::Background,pixmap);
    this->setPalette(palette);


    //    this->model = new QStandardItemModel;//new出来一个表格模型
        this->model = new QStandardItemModel;//new出来一个表格模型
        //内容归model管，外观归tableview管

        this->model->setHorizontalHeaderItem(0,new QStandardItem("ISBN号"));//QStandardItem是单元格
        this->model->setHorizontalHeaderItem(1,new QStandardItem("书名"));
        this->model->setHorizontalHeaderItem(2,new QStandardItem("作者"));
        this->model->setHorizontalHeaderItem(3,new QStandardItem("出版社"));
        this->model->setHorizontalHeaderItem(4,new QStandardItem("进书日期"));
        this->model->setHorizontalHeaderItem(5,new QStandardItem("库存量"));
        this->model->setHorizontalHeaderItem(6,new QStandardItem("批发价"));
        this->model->setHorizontalHeaderItem(7,new QStandardItem("零售价"));

        this->ui->tableView->setModel(model);//让view显示视图

        //设置单元格宽度
        this->ui->tableView->setColumnWidth(0,100);
        this->ui->tableView->setColumnWidth(1,200);
        this->ui->tableView->setColumnWidth(2,120);
        this->ui->tableView->setColumnWidth(3,200);
        this->ui->tableView->setColumnWidth(4,100);
        this->ui->tableView->setColumnWidth(5,100);
        this->ui->tableView->setColumnWidth(6,100);
        this->ui->tableView->setColumnWidth(7,100);


}

statement::~statement()
{
    delete ui;
}

void statement::display(BookData *obj)
{
    int row = 0;
    for(int i = 0; i < 100 ;i++){//展示八个数据项
        if(obj[i].existOrNot){
            this->model->setItem(row,0,new QStandardItem(obj[i].isbn));
            this->model->setItem(row,1,new QStandardItem(obj[i].bookTitle));
            this->model->setItem(row,2,new QStandardItem(obj[i].author));
            this->model->setItem(row,3,new QStandardItem(obj[i].publisher));
            this->model->setItem(row,4,new QStandardItem(obj[i].dataAdded));
            this->model->setItem(row,5,new QStandardItem(obj[i].qtyOnHand));
            this->model->setItem(row,6,new QStandardItem(obj[i].wholesale));
            this->model->setItem(row,7,new QStandardItem(obj[i].retail));
          row++;
        }
    }
}

//总列表
void statement::on_btn_none_clicked()
{
    this->model->clear();

    this->model = new QStandardItemModel;//new出来一个表格模型
    //内容归model管，外观归tableview管

    this->model->setHorizontalHeaderItem(0,new QStandardItem("ISBN号"));//QStandardItem是单元格
    this->model->setHorizontalHeaderItem(1,new QStandardItem("书名"));
    this->model->setHorizontalHeaderItem(2,new QStandardItem("作者"));
    this->model->setHorizontalHeaderItem(3,new QStandardItem("出版社"));
    this->model->setHorizontalHeaderItem(4,new QStandardItem("进书日期"));
    this->model->setHorizontalHeaderItem(5,new QStandardItem("库存量"));
    this->model->setHorizontalHeaderItem(6,new QStandardItem("批发价"));
    this->model->setHorizontalHeaderItem(7,new QStandardItem("零售价"));

    this->ui->tableView->setModel(model);//让view显示视图

    //设置单元格宽度
    this->ui->tableView->setColumnWidth(0,100);
    this->ui->tableView->setColumnWidth(1,200);
    this->ui->tableView->setColumnWidth(2,120);
    this->ui->tableView->setColumnWidth(3,200);
    this->ui->tableView->setColumnWidth(4,100);
    this->ui->tableView->setColumnWidth(5,100);
    this->ui->tableView->setColumnWidth(6,100);
    this->ui->tableView->setColumnWidth(7,100);


    display(dataArray);

}

void statement::on_btn_sale_clicked()
{
    this->model->clear();

    this->model = new QStandardItemModel;//new出来一个表格模型
    //内容归model管，外观归tableview管

    this->model->setHorizontalHeaderItem(0,new QStandardItem("ISBN号"));//QStandardItem是单元格
    this->model->setHorizontalHeaderItem(1,new QStandardItem("书名"));
    this->model->setHorizontalHeaderItem(2,new QStandardItem("作者"));
    this->model->setHorizontalHeaderItem(3,new QStandardItem("出版社"));
    this->model->setHorizontalHeaderItem(4,new QStandardItem("进书日期"));
    this->model->setHorizontalHeaderItem(5,new QStandardItem("库存量"));
    this->model->setHorizontalHeaderItem(6,new QStandardItem("批发价"));
    this->model->setHorizontalHeaderItem(7,new QStandardItem("零售价"));
    this->model->setHorizontalHeaderItem(8,new QStandardItem("批发价总额"));

    this->ui->tableView->setModel(model);//让view显示视图

    //设置单元格宽度
    this->ui->tableView->setColumnWidth(0,100);
    this->ui->tableView->setColumnWidth(1,150);
    this->ui->tableView->setColumnWidth(2,150);
    this->ui->tableView->setColumnWidth(3,200);
    this->ui->tableView->setColumnWidth(4,100);
    this->ui->tableView->setColumnWidth(5,70);
    this->ui->tableView->setColumnWidth(6,70);
    this->ui->tableView->setColumnWidth(7,70);
    this->ui->tableView->setColumnWidth(8,100);

    int row = 0;
    for(int i = 0; i < 100 ;i++){
        if(dataArray[i].existOrNot){
            this->model->setItem(row,0,new QStandardItem(dataArray[i].isbn));
            this->model->setItem(row,1,new QStandardItem(dataArray[i].bookTitle));
            this->model->setItem(row,2,new QStandardItem(dataArray[i].author));
            this->model->setItem(row,3,new QStandardItem(dataArray[i].publisher));
            this->model->setItem(row,4,new QStandardItem(dataArray[i].dataAdded));
            this->model->setItem(row,5,new QStandardItem(dataArray[i].qtyOnHand));
            this->model->setItem(row,6,new QStandardItem(dataArray[i].wholesale));
            this->model->setItem(row,7,new QStandardItem(dataArray[i].retail));

            double AllWholesale = dataArray[i].wholesale.toDouble() * dataArray[i].qtyOnHand.toInt();
            QString A_Wholesale = QString::number(AllWholesale,'f',2);
            this->model->setItem(row,8,new QStandardItem(A_Wholesale));
          row++;
        }
    }
}

//按批发价列表
void statement::on_btn_retail_clicked()
{
    this->model->clear();

    this->model = new QStandardItemModel;//new出来一个表格模型
    //内容归model管，外观归tableview管

    this->model->setHorizontalHeaderItem(0,new QStandardItem("ISBN号"));//QStandardItem是单元格
    this->model->setHorizontalHeaderItem(1,new QStandardItem("书名"));
    this->model->setHorizontalHeaderItem(2,new QStandardItem("作者"));
    this->model->setHorizontalHeaderItem(3,new QStandardItem("出版社"));
    this->model->setHorizontalHeaderItem(4,new QStandardItem("进书日期"));
    this->model->setHorizontalHeaderItem(5,new QStandardItem("库存量"));
    this->model->setHorizontalHeaderItem(6,new QStandardItem("批发价"));
    this->model->setHorizontalHeaderItem(7,new QStandardItem("零售价"));
    this->model->setHorizontalHeaderItem(8,new QStandardItem("零售价总额"));


    this->ui->tableView->setModel(model);//让view显示视图

    //设置单元格宽度
    this->ui->tableView->setColumnWidth(0,100);
    this->ui->tableView->setColumnWidth(1,150);
    this->ui->tableView->setColumnWidth(2,150);
    this->ui->tableView->setColumnWidth(3,200);
    this->ui->tableView->setColumnWidth(4,100);
    this->ui->tableView->setColumnWidth(5,70);
    this->ui->tableView->setColumnWidth(6,70);
    this->ui->tableView->setColumnWidth(7,70);
    this->ui->tableView->setColumnWidth(8,100);

    int row = 0;
    for(int i = 0; i < 100 ;i++){
        if(dataArray[i].existOrNot){
            this->model->setItem(row,0,new QStandardItem(dataArray[i].isbn));
            this->model->setItem(row,1,new QStandardItem(dataArray[i].bookTitle));
            this->model->setItem(row,2,new QStandardItem(dataArray[i].author));
            this->model->setItem(row,3,new QStandardItem(dataArray[i].publisher));
            this->model->setItem(row,4,new QStandardItem(dataArray[i].dataAdded));
            this->model->setItem(row,5,new QStandardItem(dataArray[i].qtyOnHand));
            this->model->setItem(row,6,new QStandardItem(dataArray[i].wholesale));
            this->model->setItem(row,7,new QStandardItem(dataArray[i].retail));

            double Allretail = dataArray[i].retail.toDouble() * dataArray[i].qtyOnHand.toInt();
            QString A_retail = QString::number(Allretail,'f',2);
            this->model->setItem(row,8,new QStandardItem(A_retail));
          row++;
        }
    }

}

//按零售价列表
void statement::on_btn_qty_clicked()
{
    this->model->clear();

    this->model = new QStandardItemModel;//new出来一个表格模型
    //内容归model管，外观归tableview管

    this->model->setHorizontalHeaderItem(0,new QStandardItem("ISBN号"));//QStandardItem是单元格
    this->model->setHorizontalHeaderItem(1,new QStandardItem("书名"));
    this->model->setHorizontalHeaderItem(2,new QStandardItem("作者"));
    this->model->setHorizontalHeaderItem(3,new QStandardItem("出版社"));
    this->model->setHorizontalHeaderItem(4,new QStandardItem("进书日期"));
    this->model->setHorizontalHeaderItem(5,new QStandardItem("库存量"));
    this->model->setHorizontalHeaderItem(6,new QStandardItem("批发价"));
    this->model->setHorizontalHeaderItem(7,new QStandardItem("零售价"));

    this->ui->tableView->setModel(model);//让view显示视图

    //设置单元格宽度
    this->ui->tableView->setColumnWidth(0,100);
    this->ui->tableView->setColumnWidth(1,200);
    this->ui->tableView->setColumnWidth(2,150);
    this->ui->tableView->setColumnWidth(3,200);
    this->ui->tableView->setColumnWidth(4,100);
    this->ui->tableView->setColumnWidth(5,70);
    this->ui->tableView->setColumnWidth(6,70);
    this->ui->tableView->setColumnWidth(7,70);

    BookData qty_sortArray[100];
    int max = 0;
    QString temp[8];

    for(int i = 0;i < 100;i++){
        qty_sortArray[i].qtyOnHand = dataArray[i].qtyOnHand;
        qty_sortArray[i].isbn = dataArray[i].isbn;
        qty_sortArray[i].bookTitle = dataArray[i].bookTitle;
        qty_sortArray[i].author = dataArray[i].author;
        qty_sortArray[i].publisher = dataArray[i].publisher;
        qty_sortArray[i].dataAdded = dataArray[i].dataAdded;
        qty_sortArray[i].wholesale = dataArray[i].wholesale;
        qty_sortArray[i].retail = dataArray[i].retail;
        qty_sortArray[i].existOrNot = dataArray[i].existOrNot;
    }
    int k = 0;
    while(qty_sortArray[k].isbn!="\0"){
        k++;
    }

    //按库存量从大到小排序
    for(int i = 0;i<k-1;i++){
        max = i;
        for(int j = i + 1;j < k;j++){
            if(qty_sortArray[max].qtyOnHand.toDouble() < qty_sortArray[j].qtyOnHand.toDouble()){
                max = j;//
            }//该循环结束后将得到第一轮比下来最大元素的位置max，要用
        }
        temp[0] = qty_sortArray[i].qtyOnHand;
        temp[1] = qty_sortArray[i].isbn;
        temp[2] = qty_sortArray[i].bookTitle;
        temp[3] = qty_sortArray[i].author;
        temp[4] = qty_sortArray[i].publisher;
        temp[5] = qty_sortArray[i].dataAdded;
        temp[6] = qty_sortArray[i].wholesale;
        temp[7] = qty_sortArray[i].retail;

        qty_sortArray[i].qtyOnHand = qty_sortArray[max].qtyOnHand;
        qty_sortArray[i].isbn = qty_sortArray[max].isbn;
        qty_sortArray[i].bookTitle = qty_sortArray[max].bookTitle;
        qty_sortArray[i].author = qty_sortArray[max].author;
        qty_sortArray[i].publisher = qty_sortArray[max].publisher;
        qty_sortArray[i].dataAdded = qty_sortArray[max].dataAdded;
        qty_sortArray[i].wholesale = qty_sortArray[max].wholesale;
        qty_sortArray[i].retail = qty_sortArray[max].retail;

        qty_sortArray[max].qtyOnHand = temp[0];
        qty_sortArray[max].isbn = temp[1];
        qty_sortArray[max].bookTitle = temp[2];
        qty_sortArray[max].author = temp[3];
        qty_sortArray[max].publisher = temp[4];
        qty_sortArray[max].dataAdded = temp[5];
        qty_sortArray[max].wholesale = temp[6];
        qty_sortArray[max].retail = temp[7];
    }

    //显示在列表里
    display(qty_sortArray);




//    int row = 0;
//    for(int i = 0; i < k ;i++){
//        if(qty_sortArray[i].existOrNot){
//            this->model->setItem(row,0,new QStandardItem(qty_sortArray[i].isbn));
//            this->model->setItem(row,1,new QStandardItem(qty_sortArray[i].bookTitle));
//            this->model->setItem(row,2,new QStandardItem(qty_sortArray[i].author));
//            this->model->setItem(row,3,new QStandardItem(qty_sortArray[i].publisher));
//            this->model->setItem(row,4,new QStandardItem(qty_sortArray[i].dataAdded));
//            this->model->setItem(row,5,new QStandardItem(qty_sortArray[i].qtyOnHand));
//            this->model->setItem(row,6,new QStandardItem(qty_sortArray[i].wholesale));
//            this->model->setItem(row,7,new QStandardItem(qty_sortArray[i].retail));
//          row++;
//        }
//    }




}

void statement::on_btn_wholesale_clicked()
{
    this->model->clear();

    this->model = new QStandardItemModel;//new出来一个表格模型
    //内容归model管，外观归tableview管

    this->model->setHorizontalHeaderItem(0,new QStandardItem("ISBN号"));//QStandardItem是单元格
    this->model->setHorizontalHeaderItem(1,new QStandardItem("书名"));
    this->model->setHorizontalHeaderItem(2,new QStandardItem("作者"));
    this->model->setHorizontalHeaderItem(3,new QStandardItem("出版社"));
    this->model->setHorizontalHeaderItem(4,new QStandardItem("进书日期"));
    this->model->setHorizontalHeaderItem(5,new QStandardItem("库存量"));
    this->model->setHorizontalHeaderItem(6,new QStandardItem("批发价"));
    this->model->setHorizontalHeaderItem(7,new QStandardItem("零售价"));
    this->model->setHorizontalHeaderItem(8,new QStandardItem("批发价总额"));


    this->ui->tableView->setModel(model);//让view显示视图

    //设置单元格宽度
    this->ui->tableView->setColumnWidth(0,100);
    this->ui->tableView->setColumnWidth(1,150);
    this->ui->tableView->setColumnWidth(2,150);
    this->ui->tableView->setColumnWidth(3,200);
    this->ui->tableView->setColumnWidth(4,100);
    this->ui->tableView->setColumnWidth(5,70);
    this->ui->tableView->setColumnWidth(6,70);
    this->ui->tableView->setColumnWidth(7,70);
    this->ui->tableView->setColumnWidth(8,100);


    int max = 0;
    QString temp[9];
//    bool tempBool = true;
    BookData who_sortArray[100];

    //初始化
    for(int i = 0;i < 100;i++){
        //计算总批发价

        double Allretail = dataArray[i].retail.toDouble() * dataArray[i].qtyOnHand.toInt();
        QString A_retail = QString::number(Allretail,'f',2);
        dataArray[i].allWholeSale = A_retail;

        who_sortArray[i].qtyOnHand = dataArray[i].qtyOnHand;
        who_sortArray[i].isbn = dataArray[i].isbn;
        who_sortArray[i].bookTitle = dataArray[i].bookTitle;
        who_sortArray[i].author = dataArray[i].author;
        who_sortArray[i].publisher = dataArray[i].publisher;
        who_sortArray[i].dataAdded = dataArray[i].dataAdded;
        who_sortArray[i].wholesale = dataArray[i].wholesale;
        who_sortArray[i].retail = dataArray[i].retail;
        who_sortArray[i].allWholeSale = dataArray[i].allWholeSale;
        who_sortArray[i].existOrNot = dataArray[i].existOrNot;
    }


    int k = 0;
    while(who_sortArray[k].isbn!="\0"){
        k++;
    }

    //开始排序
    for(int i = 0;i<k-1;i++){
        max = i;
        for(int j = i + 1;j < k;j++){
            if(who_sortArray[max].allWholeSale.toDouble() < who_sortArray[j].allWholeSale.toDouble()){
                max = j;//
            }//该循环结束后将得到第一轮比下来最大元素的位置max，要用
        }
        temp[0] = who_sortArray[i].qtyOnHand;
        temp[1] = who_sortArray[i].isbn;
        temp[2] = who_sortArray[i].bookTitle;
        temp[3] = who_sortArray[i].author;
        temp[4] = who_sortArray[i].publisher;
        temp[5] = who_sortArray[i].dataAdded;
        temp[6] = who_sortArray[i].wholesale;
        temp[7] = who_sortArray[i].retail;
        temp[8] = who_sortArray[i].allWholeSale;
//        tempBool = who_sortArray[i].existOrNot;


        who_sortArray[i].qtyOnHand = who_sortArray[max].qtyOnHand;
        who_sortArray[i].isbn = who_sortArray[max].isbn;
        who_sortArray[i].bookTitle = who_sortArray[max].bookTitle;
        who_sortArray[i].author = who_sortArray[max].author;
        who_sortArray[i].publisher = who_sortArray[max].publisher;
        who_sortArray[i].dataAdded = who_sortArray[max].dataAdded;
        who_sortArray[i].wholesale = who_sortArray[max].wholesale;
        who_sortArray[i].retail = who_sortArray[max].retail;
        who_sortArray[i].allWholeSale = who_sortArray[max].allWholeSale;
//        who_sortArray[i].existOrNot = who_sortArray[max].existOrNot;

        who_sortArray[max].qtyOnHand = temp[0];
        who_sortArray[max].isbn = temp[1];
        who_sortArray[max].bookTitle = temp[2];
        who_sortArray[max].author = temp[3];
        who_sortArray[max].publisher = temp[4];
        who_sortArray[max].dataAdded = temp[5];
        who_sortArray[max].wholesale = temp[6];
        who_sortArray[max].retail = temp[7];
        who_sortArray[max].allWholeSale = temp[8];
    }

//        display(who_sortArray);
    int row = 0;
    for(int i = 0; i < 100 ;i++){
        if(who_sortArray[i].existOrNot){
            this->model->setItem(row,8,new QStandardItem(who_sortArray[i].allWholeSale));
            this->model->setItem(row,0,new QStandardItem(who_sortArray[i].isbn));
            this->model->setItem(row,1,new QStandardItem(who_sortArray[i].bookTitle));
            this->model->setItem(row,2,new QStandardItem(who_sortArray[i].author));
            this->model->setItem(row,3,new QStandardItem(who_sortArray[i].publisher));
            this->model->setItem(row,4,new QStandardItem(who_sortArray[i].dataAdded));
            this->model->setItem(row,5,new QStandardItem(who_sortArray[i].qtyOnHand));
            this->model->setItem(row,6,new QStandardItem(who_sortArray[i].wholesale));
            this->model->setItem(row,7,new QStandardItem(who_sortArray[i].retail));
          row++;
        }
    }


}

//按进书日期列表
/* 比较日期函数*/
bool statement::compareDate(QString day, QString another)
{
        //分割
        QStringList subsDay = day.split('-');
        QStringList subsAnother = another.split('-');

        int dayNum = subsDay.at(0).toInt()*10000 + subsDay.at(1).toInt()*100 + subsDay.at(2).toInt();
        int anotherDayNum = subsAnother.at(0).toInt()*10000 + subsAnother.at(1).toInt()*100 + subsAnother.at(2).toInt();

        if(dayNum < anotherDayNum)
            return true;

        return false;

}


void statement::on_btn_time_clicked()
{
    BookData timeArray[100];

    this->model->clear();

    this->model = new QStandardItemModel;//new出来一个表格模型
    //内容归model管，外观归tableview管

    this->model->setHorizontalHeaderItem(0,new QStandardItem("ISBN号"));//QStandardItem是单元格
    this->model->setHorizontalHeaderItem(1,new QStandardItem("书名"));
    this->model->setHorizontalHeaderItem(2,new QStandardItem("作者"));
    this->model->setHorizontalHeaderItem(3,new QStandardItem("出版社"));
    this->model->setHorizontalHeaderItem(4,new QStandardItem("进书日期"));
    this->model->setHorizontalHeaderItem(5,new QStandardItem("库存量"));
    this->model->setHorizontalHeaderItem(6,new QStandardItem("批发价"));
    this->model->setHorizontalHeaderItem(7,new QStandardItem("零售价"));

    this->ui->tableView->setModel(model);//让view显示视图

    //设置单元格宽度
    this->ui->tableView->setColumnWidth(0,100);
    this->ui->tableView->setColumnWidth(1,150);
    this->ui->tableView->setColumnWidth(2,150);
    this->ui->tableView->setColumnWidth(3,200);
    this->ui->tableView->setColumnWidth(4,100);
    this->ui->tableView->setColumnWidth(5,95);
    this->ui->tableView->setColumnWidth(6,95);
    this->ui->tableView->setColumnWidth(7,95);


    //初始化
    for(int i = 0;i < 100;i++){
        timeArray[i].qtyOnHand = dataArray[i].qtyOnHand;
        timeArray[i].isbn = dataArray[i].isbn;
        timeArray[i].bookTitle = dataArray[i].bookTitle;
        timeArray[i].author = dataArray[i].author;
        timeArray[i].publisher = dataArray[i].publisher;
        timeArray[i].wholesale = dataArray[i].wholesale;
        timeArray[i].retail = dataArray[i].retail;
        timeArray[i].dataAdded = dataArray[i].dataAdded;
        timeArray[i].existOrNot = dataArray[i].existOrNot;
//        timeArray[i].QtimeToNum = 0;
    }

    int k = 0;
    while(dataArray[k].isbn!="\0"){
        k++;
    }


    //下面排序
    int max = 0;
    QString temp[9];

        for(int i = 0;i<k-1;i++){
            max = i;
            for(int j = i + 1;j < k;j++){
                if(compareDate(timeArray[max].dataAdded,timeArray[j].dataAdded)){
                    max = j;//
                }
            }
            temp[0] = timeArray[i].qtyOnHand;
            temp[1] = timeArray[i].isbn;
            temp[2] = timeArray[i].bookTitle;
            temp[3] = timeArray[i].author;
            temp[4] = timeArray[i].publisher;
            temp[5] = timeArray[i].dataAdded;
            temp[6] = timeArray[i].wholesale;
            temp[7] = timeArray[i].retail;

            timeArray[i].qtyOnHand = timeArray[max].qtyOnHand;
            timeArray[i].isbn = timeArray[max].isbn;
            timeArray[i].bookTitle = timeArray[max].bookTitle;
            timeArray[i].author = timeArray[max].author;
            timeArray[i].publisher = timeArray[max].publisher;
            timeArray[i].dataAdded = timeArray[max].dataAdded;
            timeArray[i].wholesale = timeArray[max].wholesale;
            timeArray[i].retail = timeArray[max].retail;

            timeArray[max].qtyOnHand = temp[0];
            timeArray[max].isbn = temp[1];
            timeArray[max].bookTitle = temp[2];
            timeArray[max].author = temp[3];
            timeArray[max].publisher = temp[4];
            timeArray[max].dataAdded = temp[5];
            timeArray[max].wholesale = temp[6];
            timeArray[max].retail = temp[7];
        }

        display(timeArray);


}

void statement::on_btn_over_clicked()
{
    this->close();
}
